﻿//注册用户的信息：[user_name, user_account]
var registerInfos = [];
//保存当前点击的是哪个用户的信息
var clickUserAccount;
//post请求后台查询user_type为空的用户，返回用户的user_name, user_account
$.ajax({
    url: '/registerInfoDepute',
    type: 'post',
    data: {'requestType': '查询注册用户列表'},
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            console.log('共取到 ' + data.length + ' 条数据');
            //将取到的所有数据循环添加到registerInfos中
            for (var i = 0; i < data.length; i++){
                registerInfos[i] = [data[i].user_name, data[i].user_account];
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            // alert('请求错误，刷新后再试')
        }
    }
});

//DDF
registerInfos = [1, 111111];
var pageID = 1;
//列数 = 2
var methodCount = 2;
//每页显示10条数据
//设置每一页的行数
var countPerPage=[];

//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})
//下面的分页框最多显示8
var pageButtonCount = 8;
var pageCount = parseInt(registerInfos.length / countPerPage) + 1;
var table = document.getElementById("registerInfos");
var tbody = table.children[1];
var pages = document.getElementById("registerInfosPages");
var inner = "<td>&nbsp;</td>";
for (var j = 0; j < methodCount; j++) {
    inner += "<td />";
}
for (var j = 0; j < countPerPage; j++) {
    var tr = document.createElement("tr");
    tr.innerHTML = inner;
    tbody.appendChild(tr);
}


for (var i = 0; i < (pageCount > pageButtonCount ? pageButtonCount : pageCount); i++) {
    var li = document.createElement("li");
    li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + 0 + ")");
    var a = document.createElement("a");
    a.innerHTML = i + 1;
    a.setAttribute("href", "#");
    li.appendChild(a);
    pages.insertBefore(li, pages.lastElementChild);
}
OnClick(1, 0);

// 生成翻页按钮
function OnClick(index, cardIndex) {
    if (index === -1) {
        if (pageID > 1) OnClick(pageID - 1, cardIndex);
        return;
    }
    if (index === -2) {
        if (pageID < pageCount) OnClick(pageID + 1, cardIndex);
        return;
    }
    pageID= index;
    //显示数据
    for (var i = 0; i < countPerPage; i++) {
        if (registerInfos[(index - 1) * countPerPage + i] !== undefined) {
            for (var j = 0; j < methodCount; j++) {
                tbody.children[i].children[j].innerHTML = registerInfos[(index - 1) * countPerPage + i][j];
            }
            var a = document.createElement('a');
            var node = document.createTextNode("分配角色");
            a.appendChild(node);
            a.setAttribute('href', '#');
            a.setAttribute('data-toggle', 'modal');
            a.setAttribute('data-target', '#myModal');
            var attr = "";
            for (j = 0; j < methodCount; j++) {
                attr += j === 0 ? "?" : "&";
                attr += "attr" + j + "=" + registerInfos[(index - 1) * countPerPage + i][j];
            }
            // button.setAttribute("onclick", "window.location.href='/projectTemp.html" + attr + "'");
            a.setAttribute('onclick', 'saveClickInfo(' + i + ')');
            tbody.children[i].children[methodCount].innerHTML = a.outerHTML;
        } else {
            tbody.children[i].children[0].innerHTML = "&nbsp;";
            for (j = 1; j < methodCount+ 1; j++) {
                tbody.children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount > pageButtonCount) {
        var first = parseInt(pageID - pageButtonCount / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount - pageButtonCount + 1) first = pageCount - pageButtonCount + 1;
        for (i = 0; i < pageButtonCount; i++) {
            pages.removeChild(pages.children[1]);
        }
        for (i = first; i < first + pageButtonCount; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + "," + 0 + ")");
            if (i === pageID)
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages.insertBefore(li, pages.lastElementChild);
        }
    } else {
        for (i = 0; i < pageCount; i++) {
            pages.removeChild(pages.children[1]);
        }
        for (i = 0; i < pageCount; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + 0 + ")");
            if (i + 1 === pageID)
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages.insertBefore(li, pages.lastElementChild);
        }
    }
}


//获取弹窗中选择的角色信息，当点击确定时，ajax请求后台修改user_type
function registerInfoBtnClick(obj) {
    //默认是客户角色
    var selectType = 3
    if (obj.id == 'fenpeijuese') {
        var temp = document.getElementById('juese');
        var index = temp.selectedIndex;
        var value = temp.options[index].value;
        console.log(value);
        switch (value) {
            case '检测主管':
                selectType = 1;
                break;
            case '审核员':
                selectType = 2;
                break;
            case '普通用户':
                selectType = 3;
                break;
            case '业务部门':
                selectType = 4;
                break;
            case '检测员':
                selectType = 5;
                break;
        }
        console.log('select=' + selectType)
        $.ajax({
            url: '/registerInfoDepute',
            type: 'post',
            data: {'requestType': '修改用户角色', 'selectAccount': clickUserAccount, 'selectType': selectType},
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    //修改成功后刷新页面
                    window.location.href = 'registerInfo.html'

                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert('修改失败！')
                }
            }
        });
    }
}
//保存当前点击的用户手机信息，通过该信息唯一确定用户
function saveClickInfo(i) {
    console.log('userAccount:' + registerInfos[i][1]);
    clickUserAccount = registerInfos[i][1];
}